package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.MessageQuestionnaireMapper;
import com.hamster.crmpre.mapper.PersonalMessageMapper;
import com.hamster.crmpre.pojo.MessageQuestionnaire;
import com.hamster.crmpre.pojo.PersonalMessage;
import com.hamster.crmpre.service.MessageQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2019/12/26 16:02
 * @description：消息查询实现类
 * @modified By：
 * @version: 1.0
 */
@Service
public class MessageQueryServiceImpl implements MessageQueryService {

    @Autowired
    private PersonalMessageMapper personalMessageMapper;
    @Autowired
    private MessageQuestionnaireMapper messageQuestionnaireMapper;
    @Autowired
    private MessageQueryService messageQueryService;



    /**
     * 修改状态为已读
     * @param id 消息中心的id
     */
    @Override
    public void setRead(String id) {
        personalMessageMapper.updateReadById(id);
    }

    /**
     * 提交问卷
     * @param messageQuestionnaire 问卷对象
     */
    @Override
    @Transactional
    public void messageQuestionnaireSubmit(String personalMessageId,MessageQuestionnaire messageQuestionnaire) {
        messageQuestionnaire.setState("1");//设置状态为  已提交
        messageQuestionnaire.setSubmitTime(new Date());//问卷提交时间
        messageQueryService.setRead(personalMessageId);//标记消息已读
        messageQuestionnaireMapper.updateById(messageQuestionnaire);
    }

    /**
     * 员工查看自己，客户提交的问卷
     * @param userId
     * @return
     */
    @Override
    public List<MessageQuestionnaire> listMessageQuestionnaireResults(String userId) {
        QueryWrapper<MessageQuestionnaire> messageQuestionnaireQueryWrapper = new QueryWrapper<>();
        messageQuestionnaireQueryWrapper.eq("employee_id",userId);
        return messageQuestionnaireMapper.selectList(messageQuestionnaireQueryWrapper);
    }

    /**
     * 员工查看客户的问卷详情
     * @param id 问卷详情结果的id
     * @return
     */
    @Override
    public MessageQuestionnaire getMessageQuestionnaireDetail(String id) {
        return messageQuestionnaireMapper.selectById(id);
    }

    /**
     * 获取未读消息数
     * @param receiverId
     * @param department
     * @return
     */
    @Override
    public Integer listQueryFinanceCount(String receiverId, String department) {
        QueryWrapper<PersonalMessage> queryWrapper = new QueryWrapper<>();
        switch (department){
            case "1" :
                department = "开发部";
                break;
            case "2" :
                department = "服务部";
                break;
            case "3" :
                department = "事业部";
                break;
            case "4" :
                department = "财务部";
                break;
            case "5" :
                department = "后勤部";
                break;
            case "6" :
                department = "人事部";
                break;
        }

        queryWrapper.eq("receiver_id",receiverId).eq("state","0");
        queryWrapper.or().eq("receiver_id",department).eq("state","0");
        queryWrapper.or().eq("department",department).eq("state","0");
        Integer integer = personalMessageMapper.selectCount(queryWrapper);
        return integer;

    }

    /**
     * 查询未读个数
     * @param receiverId
     * @return
     */
    @Override
    public Integer listCount(String receiverId) {
        QueryWrapper<PersonalMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id",receiverId).eq("state","0");
        List<PersonalMessage> personalMessages = personalMessageMapper.selectList(queryWrapper);
        if (personalMessages == null){
            return 0;
        }else {
            return personalMessages.size();
        }
    }


    /**
     * 根据信息接收者id查询到个人信息
     * @param receiverId 接收者id
     * @return 个人信息列表
     */
    @Override
    public List<PersonalMessage> list(String receiverId,String name) {
        QueryWrapper<PersonalMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id",receiverId).orderByDesc("create_time");
        if (StringUtils.isNotBlank(name)){
            queryWrapper.like("sender_name","%"+name+"%");
        }
        return personalMessageMapper.selectList(queryWrapper);
    }

    /**
     * 根据receiverId和department查询具体部门的消息
     * @param receiverId 接收者id
     * @param department 部门
     * @return 该部门用户所能接收的消息
     */
    @Override
    public List<PersonalMessage> listQueryFinance(String receiverId, String department,String name) {
        QueryWrapper<PersonalMessage> queryWrapper = new QueryWrapper<>();
        switch (department){
            case "1" :
                department = "开发部";
                break;
            case "2" :
                department = "服务部";
                break;
            case "3" :
                department = "事业部";
                break;
            case "4" :
                department = "财务部";
                break;
            case "5" :
                department = "后勤部";
                break;
            case "6" :
                department = "人事部";
                break;
        }
        if (StringUtils.isNotBlank(name)){
            queryWrapper.eq("receiver_id",receiverId).like("sender_name","%"+name+"%");
            queryWrapper.or().eq("receiver_id",department).like("sender_name","%"+name+"%");
            queryWrapper.or().eq("department",department).like("sender_name","%"+name+"%");
        }else {
            queryWrapper.eq("receiver_id",receiverId).or().eq("receiver_id",department).or().eq("department",department);
        }
       queryWrapper.orderByDesc("create_time");
        return personalMessageMapper.selectList(queryWrapper);
    }

    /**
     * 根据id返回一条用户消息
     * @param id 用户id
     * @return 一条用户消息
     */
    @Override
    public PersonalMessage get(String id) {
        return personalMessageMapper.selectById(id);
    }

    /**
     * 查询问卷详情
     * @param id 问卷详情表 id
     * @return
     */
    @Override
    public MessageQuestionnaire getMessageQuestionnaire(String id) {
       return messageQuestionnaireMapper.selectById(id);
    }



}
